package com.example.studyproject.QRCode;

import cn.hutool.core.util.CharsetUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;


/**
 * @program: StudyProject
 * @description: 二维码业务实现类
 * @author: kangwei
 * @create: 2024-08-13 10:39
 **/

@Service
public class QRCodeServiceImpl implements QRCodeService {
    private static final Logger log = LoggerFactory.getLogger(QRCodeServiceImpl.class);

    @Autowired
    private QRCodeHandler qrCodeHandler;

    @Override
    public String generateQRCode(QRCodeInfo qrCodeInfo) {
        // 生成二维码
        Map<EncodeHintType, Object> hintTypeObjectMap = new HashMap<>(16);
        hintTypeObjectMap.put(EncodeHintType.CHARACTER_SET, CharsetUtil.CHARSET_UTF_8);
        try {
            BitMatrix matrix = new MultiFormatWriter().encode(qrCodeInfo.getContent(),
                    BarcodeFormat.QR_CODE, qrCodeInfo.getWidth(), qrCodeInfo.getHeight(), hintTypeObjectMap);
            // 将二维码写入指定目路径
            Path path = FileSystems.getDefault().getPath(qrCodeInfo.getFilePath());
            MatrixToImageWriter.writeToPath(matrix, "png", path);
        } catch (WriterException e) {
            log.error("生成二维码异常", e);
        } catch (IOException e) {
            log.error("二维码图片写入异常", e);
        }
        return "000000";
    }

    @Override
    public ByteArrayResource generateQRCodeV2(QRCodeInfo qrCodeInfo) {
        Map<EncodeHintType, Object> hintTypeObjectMap = qrCodeHandler.getHits();
        try {
            BitMatrix matrix = new MultiFormatWriter().encode(qrCodeInfo.getContent(),
                    BarcodeFormat.QR_CODE, qrCodeInfo.getWidth(), qrCodeInfo.getHeight(), hintTypeObjectMap);
            BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(matrix);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "PNG", byteArrayOutputStream);
            byte[] qrCodeBytes = byteArrayOutputStream.toByteArray();

//            HttpHeaders headers = new HttpHeaders();
//            headers.add(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE);
//            ResponseEntity<ByteArrayResource> body = ResponseEntity.ok()
//                    .headers(headers)
//                    .contentLength(qrCodeBytes.length)
//                    .body(new ByteArrayResource(qrCodeBytes));
            return new ByteArrayResource(qrCodeBytes);
        } catch (WriterException e) {
            log.error("生成二维码异常", e);
        } catch (IOException e) {
            log.error("二维码图片写入异常", e);
        }
        return null;
    }
}
